
/**
 * Created by Chen Sanjin on 2020/12/12.
 */

/**
* 获取特定格式时间
* @param {Object} date 时间
* @param {String} format 格式
* @return {String} 特定格式的时间
*
* 例如：
* var now = new Date(); // Mon Jan 16 2017 14:32:22 GMT+0800 (中国标准时间)
* formatDate(now, 'yyyy-MM-dd h:m:s'); // 2017-01-16 14:32:22
*/
export function formatDate (date, format) {
  const time = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    'S+': date.getMilliseconds()
  }

  if (/(y+)/i.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (const k in time) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? time[k] : ('00' + time[k]).substr(('' + time[k]).length))
    }
  }
  return format
}
/*
 * 获取当前月的第一天
 */
export function getCurrentMonthFirstDay () {
  var date = new Date()
  var monthStartDate = new Date(date.getFullYear(), date.getMonth(), 1)
  return monthStartDate
}

/*
   * 获取当前月的最后一天
   */
export function getCurrentMonthLastDay () {
  var date = new Date()
  var monthEndDate = new Date(date.getFullYear(), date.getMonth() + 1, 0)
  return monthEndDate
}

export function getCurrentMonthFirstStr () {
  var date = new Date()
  date.setDate(1)
  var month = parseInt(date.getMonth() + 1)
  var day = date.getDate()
  if (month < 10) {
    month = '0' + month
  }
  if (day < 10) {
    day = '0' + day
  }
  return date.getFullYear() + '-' + month + '-' + day
}

/**
 * @Author: Chen Chen
 * @Date: 2020-06-09 12:14:59
 * @description: 根据日期返回字符串日期 并设置指定格式
 * @param {type}
 * @return:
 */
export function dateToStr (date, separator) {
  if (date instanceof Date) {
    var month = (date.getMonth() + 1)
    var day = date.getDate()
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    return date.getFullYear() + separator + month + separator + day
  }
  return date
}

export function getDefaultDateString (date) {
  const separator = '-'
  if (date instanceof Date) {
    var month = (date.getMonth() + 1)
    var day = date.getDate()
    if (month < 10) {
      month = '0' + month
    }
    if (day < 10) {
      day = '0' + day
    }
    return date.getFullYear() + separator + month + separator + day
  }
  return date
}

/**
 * 返回一个新日期对象, 为参数日期的年月日, 时分秒等全部变成0
 * @param {*} date
 */
export function getDateByDateTime (date) {
  const format = 'yyyy-MM-dd'
  return stringToDate(formatDate(date, format))
}

export function getDefaultDateTimeString (date) {
  const format = 'yyyy-MM-dd h:m:s'
  return formatDate(date, format)
}

export function getDateString (date, pattern) {
  const format = pattern || 'yyyy-MM-dd h:m:s'
  return formatDate(date, format)
}

export function stringToDate (time) {
  // var time = "2017-06-23 17:00:00";
  time = time + ' 00:00:00'
  time = time.replace(/-/g, ':').replace(' ', ':')
  time = time.split(':')
  var date = new Date(time[0], (time[1] - 1), time[2], time[3], time[4], time[5])
  return date
}

// function getCurrentMonthLastStr() {
//   var date = new Date();
//   var currentMonth = date.getMonth();
//   var nextMonth = ++currentMonth;
//   var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
//   var oneDay = 1000 * 60 * 60 * 24;
//   var lastTime = new Date(nextMonthFirstDay - oneDay);
//   var month = parseInt(lastTime.getMonth() + 1);
//   var day = lastTime.getDate();
//   if (month < 10) {
//     month = '0' + month
//   }
//   if (day < 10) {
//     day = '0' + day
//   }
//   return date.getFullYear() + '-' + month + '-' + day;
// }

/*
 * 获取当前的时间
 */
export function getNowTime () {
  const now = new Date()
  const year = now.getFullYear() // 得到年份
  let month = now.getMonth() // 得到月份
  let date = now.getDate() // 得到日期
  month = month + 1
  month = month.toString().padStart(2, '0')
  date = date.toString().padStart(2, '0')
  const defaultDate = `${year}-${month}-${date}`
  return defaultDate
}
/*
 * 获取七天前的时间
 */
export function getSevenDayBeforeTime () {
  const now = new Date(new Date().getTime() - 7 * 24 * 60 * 60 * 1000)
  const year = now.getFullYear() // 得到年份
  let month = now.getMonth() // 得到月份
  let date = now.getDate() // 得到日期
  month = month + 1
  month = month.toString().padStart(2, '0')
  date = date.toString().padStart(2, '0')
  const defaultDate = `${year}-${month}-${date}`
  return defaultDate
}
/*
 * 叠加时间
 */
export function plusDate (time, mile) {
  const now = new Date(time.getTime() + mile)
  const year = now.getFullYear() // 得到年份
  let month = now.getMonth() // 得到月份
  let date = now.getDate() // 得到日期
  month = month + 1
  month = month.toString().padStart(2, '0')
  date = date.toString().padStart(2, '0')
  const defaultDate = `${year}-${month}-${date}`
  return defaultDate
}

export default {
  getCurrentMonthFirstStr,
  getCurrentMonthLastDay,
  getCurrentMonthFirstDay,
  dateToStr,
  stringToDate,
  formatDate
}
